﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Adex;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using MediaMonitor.Common;
using System.Net;

namespace MediaMonitor.SiteFiles
{
    public partial class NewsAlertPress : Adex.BasePage
    {
        public string curVideo;
        public int width = 600;
        public int height = 700;
        public string ext;
        string[] name;
        public string filename;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string ID = Utility.GetRequestedVal("ID", "0");
                string imgType = Utility.GetRequestedVal("t", "s");

                width = Utility.ToInt(Utility.GetRequestedVal("w", width.ToString())) - 20;
                height = Utility.ToInt(Utility.GetRequestedVal("h", height.ToString())) - 25;

                string pressFile = Utility.GetRequestedVal("file", "");

                TblNewsAlert pv = new TblNewsAlert(ID);

                if (imgType == "s")
                {
                    if (Utility.ToStr(pv.ImageAudioVideoName) != "" && CheckPressAvailability(pressFile))
                    {
                        curVideo = pv.ImageAudioVideoName.ToString();

                        ext = curVideo.Substring(curVideo.Length - 3);
                        if (curVideo.Substring(curVideo.Length - 3).ToLower() == "pdf")
                        {
                            string path = Server.MapPath("../NewsAlert/Images/" + curVideo);
                            WebClient client = new WebClient();
                            Byte[] buffer = client.DownloadData(path);

                            if (buffer != null)
                            {
                                // context.Response.AddHeader("Content-Disposition", "attachment=" + path);
                                Response.ContentType = "application/pdf";
                                Response.AddHeader("content-length", buffer.Length.ToString());
                                Response.BinaryWrite(buffer);
                            }

                        }
                        else
                        {
                            filename = "../NewsAlert/Images/" + pressFile;
                            //width = 600;
                            //height = 700;
                            pnlVideo.Visible = true;
                            pnlNoVideo.Visible = false;
                        }
                    }
                    else
                    {
                        pnlNoVideo.Visible = true;
                        lblMsg.Text = "Picture not found";
                        pnlVideo.Visible = false;
                    }
                }
                else
                {
                    //only admin can view full sized videos
                    string[] uRoles = AppUser.GetUserRoles(AppUser.CurrUserID);
                    bool allowed = false;

                    if (uRoles.Length > 0)
                        if (uRoles[0] == "admin")
                            allowed = true;

                    string odid = Utility.GetRequestedVal("ID", "0");
                    TblNewsAlert ordItm = new TblNewsAlert(odid);
                    if (ordItm.Id.ToString() == ID)
                        allowed = true;

                    if (imgType == "f" && allowed == true)
                    {
                        if (Utility.ToStr(pressFile) != "" && CheckPressAvailability(pressFile))
                        {
                            curVideo = pv.ImageAudioVideoName.ToString();

                            ext = curVideo.Substring(curVideo.Length- 3);
                            if (curVideo.Substring(curVideo.Length - 3) == "PDF")
                            {
                                string path = Server.MapPath("../NewsAlert/Images/" + curVideo);
                                WebClient client = new WebClient();
                                Byte[] buffer = client.DownloadData(path);

                                if (buffer != null)
                                {
                                    Response.ContentType = "application/pdf";
                                    Response.AddHeader("content-length", buffer.Length.ToString());
                                    Response.BinaryWrite(buffer);
                                }

                            }
                            else
                            {
                                filename = "../NewsAlert/Images/" + pressFile;
                                pnlVideo.Visible = true;
                                pnlNoVideo.Visible = false;
                            }
                            //width = 600;
                            //height = 700;
                        }
                        else
                        {
                            pnlNoVideo.Visible = true;
                            lblMsg.Text = "Press Picture not found";
                            pnlVideo.Visible = false;
                        }
                    }
                    else
                    {
                        pnlNoVideo.Visible = true;
                        lblMsg.Text = "You dont have permissions to see the full size Press Image";
                        pnlVideo.Visible = false;
                    }
                }

            }
           
            
        }

        private bool CheckPressAvailability(string fileName)
        {
            return File.Exists(Server.MapPath("../NewsAlert/Images/" + fileName));
        }
    }
}